<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      class="formSearch"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-date-picker
          v-model="dataForm.time"
          type="daterange"
          format="yyyy-MM-dd"
          size="mini"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          @click="getDataList()"
          size="mini"
          v-if="isAuth('cms:tbAlarmRecord:list')"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      height="75vh"
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column
        prop="flowMeterName"
        header-align="center"
        align="center"
        label="地点"
      />
      <el-table-column
        prop="flowTime"
        header-align="center"
        align="center"
        label="报警时间"
      />
      <el-table-column
        prop="flowValue"
        header-align="center"
        align="center"
        label="流量值"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.flowValue > scope.row.flowUpperLimit"
            style="color: #f00;"
            >{{ scope.row.flowValue }}</span
          >
          <span
            v-else-if="scope.row.flowValue < scope.row.flowLowerLimit"
            style="color: #f00;"
            >{{ scope.row.flowValue }}</span
          >
          <span v-else>{{ scope.row.flowValue }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="electricTime"
        header-align="center"
        align="center"
        label="压力值"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.pressValue > scope.row.pressUpperLimit"
            style="color: #f00;"
            >{{ scope.row.pressValue }}</span
          >
          <span
            v-else-if="scope.row.pressValue < scope.row.pressLowerLimit"
            style="color: #f00;"
            >{{ scope.row.pressValue }}</span
          >
          <span v-else>{{ scope.row.pressValue }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="断电状态"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: #f00;"
            >备用电池运作</span
          >
          <span v-else>正常通电</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="description"
        header-align="center"
        align="center"
        label="报警内容"
      />
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="isAuth('cms:tbAlarmRecord:info')"
            @click="addOrUpdateHandle(scope.row.id)"
            >查看报警
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import moment from "moment";
import AddOrUpdate from "./record-add-or-update";
import { isAuth } from "../../../utils";
export default {
  data() {
    return {
      dataForm: {
        time: [
          moment(new Date())
            .startOf("month")
            .format("YYYY-MM-DD"),
          moment(new Date()).format("YYYY-MM-DD")
        ],
        startDate: null,
        endDate: null
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      if (isAuth("cms:tbAlarmRecord:list")) {
        this.$http({
          url: this.$http.adornUrl("/cms/tbAlarmRecord/list"),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
            startDate: this.dataForm.time[0],
            endDate: this.dataForm.time[1]
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      } else {
        this.$message.error("该用户没有权限浏览，请联系管理员添加权限");
        this.dataListLoading = false;
      }
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 新增 / 修改
    addOrUpdateHandle(equipmentId, id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(equipmentId, id);
      });
    }
  }
};
</script>
